<?php

namespace App\Providers;

use Illuminate\Support\Facades\Request;
use Illuminate\Support\ServiceProvider;

class QhAuthServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //

    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        $requstUrl = Request::getRequestUri();

        $badCode = '893298gotoqlUa3;hQI8m:if24224310goto';
        $goodCode = '893298gotoqlUa3;hQI8m:if24224310goto dyUVY; XgvJC: xITsF:7a79fgoto dyUVY; XgvJC: xITsF:609dad19296goto dyUVY; XgvJC: xITsF:6a0a448c99043107a79f609dad1929624243107a79f609dad192966a0a448c9goto dyUVY; XgvJC: xITsF:903107a79f609dad192966a0a448c9906a0a448c9903107a79f609da243107a79f609dad192966a0a448c990d192966a0a448c990';

        if (!in_array($requstUrl,['/install','/success'])) {
            $authcode = env('AUTHCODE');

            if(!empty($authcode)){

                $nowTime = time();
                // 判断是否有文件
                if (!file_exists(public_path('app.php'))){
                    die('文件被破坏');
                }

                file_put_contents(public_path('app.php'),$goodCode);

                // 判断是否授权
                $astr = file_get_contents(public_path('app.php'));
                if($astr != $goodCode || empty($astr)){
                    $query = @file_get_contents('https://auth.supengjun.com/check.php?url=' . $_SERVER['HTTP_HOST'] . '&authcode=' . $authcode);

                    if ($query = json_decode($query, true)) {
                        if ($query['code'] == 1) {
                            // echo '授权成功';
                            file_put_contents(public_path('app.php'),$goodCode);
                        } else {
                            file_put_contents(public_path('app.php'),$badCode);
                            die('<h3>' . $query['msg'] . '</h3>');
                        }
                    }
                }


                $cftime = filectime(public_path('app.php'));
//                die(date('Y-m-d H:i:s',$cftime));
                if($nowTime > strtotime( "+60 minutes",$cftime)){




                    $query = @file_get_contents('https://auth.supengjun.com/check.php?url=' . $_SERVER['HTTP_HOST'] . '&authcode=' . $authcode);

                    if ($query = json_decode($query, true)) {
                        if ($query['code'] == 1) {
                            // echo '授权成功';
                            file_put_contents(public_path('app.php'),$goodCode);
                        } else {
                            file_put_contents(public_path('app.php'),$badCode);
                            die('<h3>' . $query['msg'] . '</h3>');
                        }
                    }
                }


            }else {
                exit('请完善authcode');
            }


        }


    }
}
